package com.github.live.base;

import android.content.Context;
import android.os.Looper;
import android.os.SystemClock;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.github.live.R;
import com.github.livebase.log.Logger;
import com.github.livebase.log.LoggerFactory;

/**
 * @ClassName DefaultUncaughtExceptionHandler
 * @description:
 * @author: biao532
 * @create: 2024-02-04 14:42
 * @Version 1.0
 **/
public class ProxyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    private Thread.UncaughtExceptionHandler handler;
    private Context mContext;
    public ProxyUncaughtExceptionHandler(Context context) {
        this.handler = Thread.getDefaultUncaughtExceptionHandler();
        this.mContext = context;
    }
    @Override
    public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
        Logger logger = LoggerFactory.getLogger(null);
        logger.error("UncaughtException", e, "an error occurred in thread " + t.getName());
        new Thread(()->{
            Looper.prepare();
            Toast.makeText(mContext, R.string.root_logger_error, Toast.LENGTH_LONG).show();
            Looper.loop();
        }).start();
        SystemClock.sleep(5000);
        if (this.handler != null) {
            this.handler.uncaughtException(t, e);
        }
    }
}
